<template>
	<view class="navbar">
		<view class="fixed-content" :style="{
        backgroundColor: bgColor,
        color: color,
      }">
			<!-- 状态栏高度 -->
			<view :style="{ height: statusBarHeight + 'px' }"></view>
			<!-- 导航栏高度 -->
			<view class="bar-content" :style="{ height: navbarHeight + 'px' }">
				<slot name="default">
					<view class="nav-left">
						<template v-if="isOpenLeftSolt">
							<slot name="left-arrow" />
						</template>
						<template v-else>
							<up-icon name="arrow-left" color="#ffffff" size="22" @tap="navigateBack()"></up-icon>
						</template>
					</view>
					<view class="nav-title" :style="{
			        color: color,
			      }">
						<template v-if="isOpenCenterSolt">
							<slot name="center-content" />
						</template>
						<template v-else>
							<view>
								{{ title }}
							</view>
						</template>
					</view>

					<view class="nav-right" :style="{
			        color: color,
			      }">
						<template v-if="isOpenRightSolt">
							<slot name="right-content" />
						</template>
					</view>
				</slot>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		useAppStore
	} from "@/store/app";
	export default {
		name: "navbar",
		props: {
			title: {
				type: String,
				default: "",
			},
			bgColor: {
				type: String,
				default: "#FFFFFF",
			},
			color: {
				type: String,
				default: "#FFFFFF",
			},
			// 是否开启左侧solt内容
			isOpenLeftSolt: {
				type: Boolean,
				default: () => false,
			},
			// 是否开启中间solt内容
			isOpenCenterSolt: {
				type: Boolean,
				default: () => false,
			},
			// 是否开启右侧solt内容
			isOpenRightSolt: {
				type: Boolean,
				default: () => false,
			},
		},
		data() {
			return {
				statusBarHeight: 0,
				navbarHeight: 0,
			};
		},
		mounted() {
			this.init();
		},
		methods: {
			init() {
				const store = useAppStore();
				const {
					getNavBarHeight,
					geStatusBarHeight
				} = store;
				this.navbarHeight = getNavBarHeight();
				this.statusBarHeight = geStatusBarHeight();
			},
			navigateBack() {
				uni.navigateBack();
			},
		},
	};
</script>

<style lang="scss" scoped>
	.navbar {
		position: sticky;
		top: 0;
		z-index: 999;

		.fixed-content {
			left: 0;
			width: 100%;
			padding: 0 24rpx;
			box-sizing: border-box;
			z-index: 1996;

			.bar-content {
				display: flex;
				align-items: center;
				position: relative;

				// 左侧内容
				.nav-left {
					position: absolute;
					z-index: 2;
					top: 50%;
					transform: translateY(-50%);
					display: flex;
					justify-content: flex-start;
					align-items: center;

					image {
						width: 38rpx;
						height: 38rpx;
					}
				}

				// 中间内容
				.nav-title {
					font-weight: bold;
					font-size: 16px;
					width: 100%;
					position: absolute;
					top: 50%;
					transform: translateY(-50%);
					text-align: center;
				}

				// 右侧内容
				.nav-right {
					position: absolute;
					z-index: 2;
					top: 50%;
					right: 24rpx;
					transform: translateY(-50%);
				}
			}
		}
	}
</style>